<?php

if(isset($_GET['getanime'])){
	echo json_encode(fetch_anime());
	exit();
}

if(isset($_GET['submit_chap'])){
	
	$r=array("status"=>0,"error"=>0);
	$link=$_POST['link'];
	$md=new Model;
	$md->query("select id from chapterk2 where manga=".intval($_POST['manga'])." and url='".$link."'");
	
	if($md->num_rows()>0){
		$r['status']="Ton Tai Link Roi";
	}else{
		$r['status']="OK";
	}
	
	$str2=geturl2($link);
	if(strpos($link,"vnsharing")!==false){
		$links=vnsurl($str2);
			if(mb_strpos($links,'vnsharing') !==false){
			//echo "CÓ LINK CỦA VNSHARING, XIN VUI LÒNG CHỌN NGUỒN KHÁC ĐỂ LẤY LINK NHÉ";
		
					$surl="content=".urlencode($links);
					$url = 'http://bk2.manga24h.com/index.php';
					$resultContent = mycurl($surl,$url);
					$links = urldecode($resultContent);
				
			//echo $data;
			}
	}else
	if(strpos($link,"blogtruyen")!==false){
			$html = new simple_html_dom();
			$html->load($str2);
			$links=$html->find("#content",0)->innertext;
			$links=str_replace("?imgmax=2048","",$links);
	}else
	if(strpos($link,"truyentranh8")!==false){
				//echo "CÓ LINK CỦA VNSHARING, XIN VUI LÒNG CHỌN NGUỒN KHÁC ĐỂ LẤY LINK NHÉ";
				$links=tt8_getimage($link);
				
			//echo $data;
			
	}
	
	
	//ADD DB
	$md->query('select id from chapterk2 where manga='.intval($_POST['manga']).' and thutu='.floatval($_POST['thutu']));
	if($md->num_rows()>0){
		$r['status']="Thứ Tự Tồn Tại";
		echo json_encode($r);
		exit();
	}
	//End of kiem tra thu tu chuong
	$chap_name=trim($_POST['name']);
	$data=array(
			'manga'=>intval($_POST['manga']),
			'thutu' => floatval($_POST['thutu']),
			'ten'=>trim($_POST['name']),
			'seourl'=>strtolower(khongdau(trim($_POST['manga_name']." ".$chap_name))),
			'uploader'=>'banhmi',
			'ngaythem' =>date('Y-m-d'),
			'source'=>$links,
			'url'=>$link
	);
	if($r['error']==1){
	$data['server']='error';
	$data['check_link']=1;
	}
	$md->insert('chapterk2',$data);
	$r['mysql']=mysql_error();


	
	//END ADD DB
	
	
   	echo json_encode($r);
	exit();
}

if(isset($_GET['update_anime_scan'])){
	$id=intval($_POST['id']);
	echo "Update nao ".$id;
	$db=new db;
	$db->connect();
	$db->nonquery("update anime set scan_auto=0 where id=".$id);
	$db->close();
	$cache=new cache;
	$cache->flush("anime".intval($_POST['id']),'anime');
	exit();
}


//Scan fixloi ------------

if(isset($_GET['getanime2'])){
	echo json_encode(fetch_anime2());
	exit();
}




if(isset($_GET['submit_chap2'])){
	
	
	$link=$_POST['link'];
	$md=new Model;

	
	$str2=geturl2($link);
	if(strpos($link,"vnsharing")!==false){
		$links=vnsurl($str2);
			if(mb_strpos($links,'vnsharing') !==false){
					//echo "CÓ LINK CỦA VNSHARING, XIN VUI LÒNG CHỌN NGUỒN KHÁC ĐỂ LẤY LINK NHÉ";
		
					$surl="content=".urlencode($links);
					$url = 'http://bk2.manga24h.com/index.php';
					$resultContent = mycurl($surl,$url);
					$links = urldecode($resultContent);
				
			//echo $data;
			}
	}else
	if(strpos($link,"blogtruyen")!==false){
			$html = new simple_html_dom();
			$html->load($str2);
			$links=$html->find("#content",0)->innertext;
			$links=str_replace("?imgmax=2048","",$links);
	}else
	if(strpos($link,"truyentranhnhanh")!==false){
	
			
					//echo "CÓ LINK CỦA VNSHARING, XIN VUI LÒNG CHỌN NGUỒN KHÁC ĐỂ LẤY LINK NHÉ";
					$html=str_get_html($str2);
					$links= $html->find('.neoslideshow',0)->innertext;
					$surl="content=".urlencode($links);
					$url = 'http://bk2.manga24h.com/index.php';
					$resultContent = mycurl($surl,$url);
					$links = urldecode($resultContent);
				
			//echo $data;
			
	}
	else
	if(strpos($link,"truyentranh8")!==false){
	
			
					//echo "CÓ LINK CỦA VNSHARING, XIN VUI LÒNG CHỌN NGUỒN KHÁC ĐỂ LẤY LINK NHÉ";
				$links=tt8_getimage($link);
				
			//echo $data;
			
	}
	else
	if(strpos($link,"vuitruyentranh")!==false){
			//GET VUITRUYENTRANH CHE DO MOBILE NHE MOI NGUOI
			$str=geturl2($link);
			$html=str_get_html($str);
			$str= $html->find('.swipe-wrap',0)->innertext;
			$str=str_replace('src="http://vuimanga.static.game.zing.vn/resources/vmgmw/img/loading.gif" ',' ',$str);
			$str=str_replace('data-rel="http://vuimanga.static.game.zing.vn/resources/vmgmw/img/loading.gif"','',$str);
			$data=str_replace('data-src','src',$str); 
			$surl="content=".urlencode($data);
			$url = 'http://bk2.manga24h.com/index.php';
			$resultContent = mycurl($surl,$url);
			$links = urldecode($resultContent);
		
			
	}
	
	
	
	
	
				
	
	
	
	//ADD DB
	$md->nonquery('delete from  chapterk_content where  chap_id='.intval($_POST['chap_id']));

	//End of kiem tra thu tu chuong
	
	$data=array(
			'source'=>$links
			
	);
	
	$data['server']='okay';
	$data['check_link']=0;
	$data['check_error']=0;
	$data['op']=0;
	
	$md->where='id='.$_POST['chap_id'];
	$md->update('chapterk2',$data);
	
	$r=array("status"=>0,"error"=>0);
	$r['error']=mysql_error();

	//END ADD DB
	
	
   	echo json_encode($r);
	exit();
}
if(isset($_GET['update_anime_scan2'])){
	

	$id=intval($_POST['id']);
	echo "Update nao ".$id;
	$db=new db;
	$db->connect();
	$db->nonquery("update anime set scan_auto=0 where id=".$id);
	$db->close();
	$cache=new cache;
	$cache->flush("anime".intval($_POST['id']),'anime');
	exit();
}

//End scan fix loi

?>